#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "public.h"

int trace_level = MSG_LEVEL_OFF;
char appkey[32];
char devicecode[32];
//struct queue queue_ac;
//struct queue queue_bd;

void printhelp(void)
{
    printf("Usage:read-primitive-measurement [options]\n");
    printf("options:\n");
	printf("  -a Str appkey \t\t Connect remote server\n");
	printf("  -b Num baudrate \t\t Com baudrate\n");
	printf("  -c Str device code \t\t Connect remote server\n");
    printf("  -d no debug-level \t\t Increase debug verbosity level\n");
    printf("  -D Num set-debug-level \t Set the debug verbosity level\n");
    printf("      0  minimum\n");
    printf("      1  error\n");
    printf("      2  warning\n");
    printf("      3  info\n");
    printf("      4  verbose\n");
    printf("      5  debug\n");
    printf("      6  maximum\n");
	printf("  -s Num \t\t\t first serial number\n");
	printf("  -t Num \t\t\t second serial number\n");
    printf("  -h help \t\t\t Display this information\n");
}

static int progarm_para(int argc, char ** argv, int * uart_f, int * uart_s, long * baudrate)
{
    int c;
    const char * opts;
    int level = -1;

    opts = "a:bc:dD:sth";

    while((c = getopt(argc, argv, opts)) != -1) {
        switch(c) {
		case 'a':
			strncpy(appkey, optarg, strlen(optarg));
			ac_traces(MSG_LEVEL_DEBUG, "appkey = %s\n", appkey); 

			break;

		case 'b':
			*baudrate = atol(optarg);
			ac_traces(MSG_LEVEL_DEBUG, "baudrate = %ld\n", *baudrate); 

			break;
		
		case 'c':
			strncpy(devicecode, optarg, strlen(optarg));
			ac_traces(MSG_LEVEL_DEBUG, "device code = %s\n", devicecode); 

			break;

        case 'd':
            trace_level = MSG_LEVEL_OFF;
            break;

        case 'D':
            level = atoi(optarg);
            trace_level = level;

            if (level < MSG_LEVEL_OFF)
                trace_level = MSG_LEVEL_OFF;
            else if (level > MSG_LEVEL_MAX)
                trace_level = MSG_LEVEL_MAX;

            ac_traces(MSG_LEVEL_DEBUG, "trace_level = %d level = %d\n", trace_level, level); 

            break;

		case 's':
			*uart_f = atoi(optarg);
			ac_traces(MSG_LEVEL_DEBUG, "serial number = %d\n", *uart_f); 

			break;

		case 't':
			*uart_s = atoi(optarg);
			ac_traces(MSG_LEVEL_DEBUG, "serial number = %d\n", *uart_s); 

			break;

        case 'h':
            printhelp();
            exit(1);
            break;
        }
    }

    return 0;
}

/*
 * abstruct : main program.
 */

int main(int argc, char ** argv)
{
	pthread_t ac_read_id;
	pthread_t com_send_id;
	pthread_t bd_read_id;
	pthread_t imu_tilt_read_id;
	void * ptr;
	pthread_attr_t attr;
	int uart_f = ~0;
	int uart_s = ~0;
	long baudrate = ~0;

	if (argc >= 2) {
		progarm_para(argc, argv, &uart_f, &uart_s, &baudrate);
	}
	else {
		printhelp();
		printf("\nerror : main program exit...\n");
		exit(1);
	}

	ac_traces(MSG_LEVEL_INFO, "read primitive measurement!");
	
	ac_traces(MSG_LEVEL_INFO,"uart1 = %d, uart2 = %d, baudrate = %ld\n", uart_f, uart_s, baudrate);
	//uart_init(uart_f, baudrate, 8, 1, 's', &uart_ac);	
	//uart_init(uart_s, baudrate, 8, 1, 's', &uart_bd);	
	uart_init(4, 115200, 8, 1, 's', &uart_ac);	
	uart_init(7, 115200, 8, 1, 's', &uart_bd);	
	uart_init(1, 115200, 8, 1, 's', &uart_485);

    /* init queue */
    //init(&queue_ac, 16);
    //init(&queue_bd, 16);
	
	/* file operation init */
	/* init_pm(); */

	pthread_attr_init(&attr);

	ac_read_id = 0;
	pthread_create(&ac_read_id, &attr, uartac_receive_thread, NULL);

	bd_read_id = 0;
	pthread_create(&bd_read_id, &attr, uartbd_receive_thread, NULL);

	com_send_id = 0;
	pthread_create(&com_send_id,NULL, uart_send_thread, NULL);

	imu_tilt_read_id = 0;
	pthread_create(&imu_tilt_read_id,NULL, read_imu_tilt_thread, NULL);

	pthread_join(ac_read_id, & ptr);
	pthread_join(bd_read_id, & ptr);
	pthread_join(com_send_id, & ptr);
	pthread_join(imu_tilt_read_id, & ptr);

	return 0;
}



